package io.xxx.omni.sync.impl

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.pdd.pop.sdk.http.PopHttpClient
import com.pdd.pop.sdk.http.api.pop.request.PddRefundListIncrementGetRequest
import com.pdd.pop.sdk.http.api.pop.response.PddRefundListIncrementGetResponse
import io.xxx.omni.sync.Document
import io.xxx.omni.sync.PagePorter
import io.xxx.omni.sync.Porter
import io.xxx.omni.sync.Store
import io.xxx.omni.util.dateTimeFormatter
import io.xxx.omni.util.offset
import io.xxx.omni.util.toLocalDateTime
import lombok.SneakyThrows
import org.springframework.stereotype.Component
import org.springframework.web.util.UriComponentsBuilder
import java.net.URI
import java.time.LocalDateTime

/**
 * 拼多多
 */
@Component
class PddTradePorter : Porter() {

    private val httpUrl = "http://pddoms-test.winnermedical.com/api/trades/stream"

    override val platformCode = "pinduoduo"

    override val jobId = 1

    override fun getUrl(
        store: Store,
        startTime: LocalDateTime,
        endTime: LocalDateTime,
        parameter: Any?
    ): URI {
        return UriComponentsBuilder.fromHttpUrl(httpUrl)
            .queryParam("startTime", startTime.format(dateTimeFormatter))
            .queryParam("endTime", endTime.format(dateTimeFormatter))
            .queryParam("shopCode", store.code)
            .build()
            .toUri()
    }

    override fun buildDocuments(data: List<String>): List<Document> {
        return data.map {
            val value = JSON.parseObject(it)
            val modified = value.getString("pdpModified").toLocalDateTime()
            Document(value.getString("orderSn"), null, value, modified)
        }
    }
}

@Component
class PddRefundPorter : PagePorter() {

    override val platformCode = "pinduoduo"

    override val jobId = 2

    override fun getCount(store: Store, startTime: LocalDateTime, endTime: LocalDateTime, parameter: Any?): Long? {
        val response = getResponse(store, startTime, endTime)
        return response.refundIncrementGetResponse?.totalCount?.toLong()
    }

    override fun getData(
        store: Store,
        startTime: LocalDateTime,
        endTime: LocalDateTime,
        parameter: Any?,
        pageNo: Long,
    ): List<Document>? {
        val response = getResponse(store, startTime, endTime, pageNo)
        return response.refundIncrementGetResponse?.refundList?.map {
            Document(it.id.toString(), it.orderSn, JSON.toJSON(it) as JSONObject, it.updatedTime.toLocalDateTime())
        }
    }

    @SneakyThrows
    private fun getResponse(
        store: Store,
        startTime: LocalDateTime,
        endTime: LocalDateTime,
        pageNo: Long = 1
    ): PddRefundListIncrementGetResponse {
        val client = PopHttpClient(store.appKey, store.appSecret)
        val request = PddRefundListIncrementGetRequest()
        request.setAfterSalesStatus(10) // 退款成功
        request.setAfterSalesType(1) // 1-仅退款，2-退货退款，3-换货，4-补寄，5-维修
        request.setStartUpdatedAt(startTime.toEpochSecond(offset))
        request.setEndUpdatedAt(endTime.toEpochSecond(offset))
        request.setPage(pageNo.toInt())
        request.setPageSize(pageSize)
        return client.syncInvoke(request, store.accessToken)
    }
}